﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace Koala.Pro.Controls
{
    public partial class NumericRange : UserControl
    {
        public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(NumericRange), new FrameworkPropertyMetadata("文本说明", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

        public static readonly DependencyProperty StartValueProperty = DependencyProperty.Register("StartValue", typeof(decimal?), typeof(NumericRange), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnStartValuePropertyChanged));

        public static readonly DependencyProperty EndValueProperty = DependencyProperty.Register("EndValue", typeof(decimal?), typeof(NumericRange), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnEndValuePropertyChanged));

        public static readonly RoutedEvent ValueChangedEvent;

        public static readonly DependencyProperty ValueChangedCommandProperty;

        public string Text
        {
            get
            {
                return (string)GetValue(TextProperty);
            }
            set
            {
                SetValue(TextProperty, value);
            }
        }

        public decimal? StartValue
        {
            get
            {
                return (decimal?)GetValue(StartValueProperty);
            }
            set
            {
                SetValue(StartValueProperty, value);
            }
        }

        public decimal? EndValue
        {
            get
            {
                return (decimal?)GetValue(EndValueProperty);
            }
            set
            {
                SetValue(EndValueProperty, value);
            }
        }

        public ICommand ValueChangedCommand
        {
            get
            {
                return (ICommand)GetValue(ValueChangedCommandProperty);
            }
            set
            {
                SetValue(ValueChangedCommandProperty, value);
            }
        }

        public event RoutedEventHandler ValueChanged
        {
            add
            {
                AddHandler(ValueChangedEvent, value);
            }
            remove
            {
                RemoveHandler(ValueChangedEvent, value);
            }
        }

        public NumericRange()
        {
            InitializeComponent();
        }

        private static void OnStartValuePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is NumericRange numericRange)
            {
                decimal? oldValue = (decimal?)e.OldValue;
                decimal? newValue = (decimal?)e.NewValue;
                RoutedPropertyChangedEventArgs<decimal?> routedPropertyChangedEventArgs = new RoutedPropertyChangedEventArgs<decimal?>(oldValue, newValue, NumericRange.ValueChangedEvent);
                numericRange.RaiseEvent(routedPropertyChangedEventArgs);
                numericRange.ValueChangedCommand?.Execute(routedPropertyChangedEventArgs);
            }
        }

        private static void OnEndValuePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is NumericRange numericRange)
            {
                decimal? oldValue = (decimal?)e.OldValue;
                decimal? newValue = (decimal?)e.NewValue;
                RoutedPropertyChangedEventArgs<decimal?> routedPropertyChangedEventArgs = new RoutedPropertyChangedEventArgs<decimal?>(oldValue, newValue, NumericRange.ValueChangedEvent);
                numericRange.RaiseEvent(routedPropertyChangedEventArgs);
                numericRange.ValueChangedCommand?.Execute(routedPropertyChangedEventArgs);
            }
        }

        private void txtStartEndValue_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Return && sender is TextBox textBox)
            {
                textBox.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
            }
        }

        static NumericRange()
        {
            ValueChangedEvent = EventManager.RegisterRoutedEvent("ValueChanged", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(NumericRange));
            ValueChangedCommandProperty = DependencyProperty.Register("ValueChangedCommand", typeof(ICommand), typeof(NumericRange), new PropertyMetadata(null));
        }
    }
}
